package com.ybear.ybhotrepair.utils;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class IOUtil {
    private static final String NAME_DEX_AV = "yb_hot_repair_dex_av";
    private static final String NAME_DEX_ID = "yb_hot_repair_dex_id";
    private static final String KEY_DEX_AV = "dav_";
    private static final String KEY_DEX_AV_COUNT = "dav_count";
    private static final String KEY_VER = "version";
    private static final String KEY_CHECK_ID = "check_id";

    public static boolean saveDexCheckID(@NonNull Context context, String checkID) {
        return context.getSharedPreferences(NAME_DEX_ID, Context.MODE_PRIVATE)
                .edit()
                .putString(KEY_CHECK_ID, checkID)
                .commit();
    }

    public static String readDexCheckID(@NonNull Context context) {
        return context.getSharedPreferences(NAME_DEX_ID, Context.MODE_PRIVATE)
                .getString(KEY_CHECK_ID, null);
    }

    /**
     * 清除Dex检查id
     * @param context   上下文
     * @return          清除结果
     */
    public static boolean clearDexCheckID(@NonNull Context context) {
        LogUtil.d("Clear of local dex check id.");
        return context.getSharedPreferences(NAME_DEX_ID, Context.MODE_PRIVATE)
                .edit()
                .clear()
                .commit();
    }

    /**
     * 保存所有Dex文件的校验值（摘要信息）
     * @param context   上下文
     * @param ver       补丁版本
     * @param data      校验值
     * @return          保存结果
     */
    public static boolean saveDexHashVals(@NonNull Context context,
                                               String ver, @NonNull String... data) {
        SharedPreferences.Editor edit;
        if( data.length == 0 ) return false;

        edit = context.getSharedPreferences(NAME_DEX_AV, Context.MODE_PRIVATE).edit();
        //校验值数量
        edit.putInt(KEY_DEX_AV_COUNT, data.length);
        //当前补丁版本
        edit.putString(KEY_VER, ver );
        //校验值
        for (int i = 0; i < data.length; i++) {
            edit.putString(KEY_DEX_AV + i, data[ i ]);
        }
        return edit.commit();
    }

    /**
     * 获取所有Dex文件的校验值（摘要信息）
     * @param context   上下文
     * @return          校验值
     */
    public static List<String> readDexHashVals(@NonNull Context context) {
        SharedPreferences sp = context.getSharedPreferences(NAME_DEX_AV, Context.MODE_PRIVATE);
        List<String> avList = new ArrayList<>();
        String ver = sp.getString(KEY_VER, null);
        int count = sp.getInt(KEY_DEX_AV_COUNT, 0);

        if( ver == null || count == 0 ) return new ArrayList<>();
        //校验值
        for (int i = 0; i < count; i++) {
            String av = sp.getString(KEY_DEX_AV + i, null);
            if( av != null ) avList.add( av );
        }
        return avList;
    }

    /**
     * 清除所有Dex补丁校验值
     * @param context   上下文
     * @return          清除结果
     */
    public static boolean clearDexHashVal(@NonNull Context context) {
        LogUtil.d("Clear of local dex hash value.");
        return context.getSharedPreferences(NAME_DEX_AV, Context.MODE_PRIVATE)
                .edit()
                .clear()
                .commit();
    }

    /**
     * 读取补丁版本
     * @param context   上下文
     * @return          版本号
     */
    @Nullable
    public static String readDexVersion(Context context) {
        return context.getSharedPreferences(NAME_DEX_AV, Context.MODE_PRIVATE)
                .getString(KEY_VER, null);
    }
}
